﻿using SmartHotel.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartHotel.DAL
{
    public class NLogDAL
    {
        public NLogDAL(HotelContext hotelContext)
        {
            HotelContext = hotelContext;
        }

        public HotelContext HotelContext { get; }

        /// <summary>
        /// 分页查询 ,返回list集合
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="count"></param>
        /// <param name="message">查询条件</param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public List<NlogModel> GetNlogModels(int index, int size, out int count, string? message, DateTime? begin, DateTime? end) 
        {
            var logs = HotelContext.NlogModels.AsQueryable();
            if (!string.IsNullOrEmpty(message)) {
                logs = logs.Where(x=>x.Massage.Contains(message));
            }
            if (begin != null) {
                logs = logs.Where(x=> begin< Convert.ToDateTime(x.Logger));
            }
            if (begin != null) {
                logs = logs.Where(x=> begin<= Convert.ToDateTime(x.Logger));
            }
            if (end != null) {
                logs = logs.Where(x=> end >= Convert.ToDateTime(x.Logger));
            }

            count = logs.Count();

            return logs.OrderByDescending(x => x.Logger)
                .Skip((index-1)*size)
                .Take(size)
                .ToList() ;
        
        
        } 
    }
}
